package com.th.cash.th2.main

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.th.resmodel.R
import com.th.resmodel.databinding.ViewStepSelectorBinding

class StepSelectorView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val binding: ViewStepSelectorBinding

    init {
        binding = ViewStepSelectorBinding.inflate(LayoutInflater.from(context), this, true)
        setupClickListeners()
    }

    private fun setupClickListeners() {
        binding.tvStep1.setOnClickListener {
            updateStepSelection(0)
            binding.ivStepContent.setImageResource(R.mipmap.img_14)
        }

        binding.tvStep2.setOnClickListener {
            updateStepSelection(1)
            binding.ivStepContent.setImageResource(R.mipmap.img_15)
        }

        binding.tvStep3.setOnClickListener {
            updateStepSelection(2)
            binding.ivStepContent.setImageResource(R.mipmap.img_16)
        }
    }

    private fun updateStepSelection(selectedIndex: Int) {
        // Reset all steps to normal state
        binding.tvStep1.apply {
            setBackgroundColor(ContextCompat.getColor(context,R.color.white))
            setTextColor(ContextCompat.getColor(context,R.color.c_1A1A1A))
        }
        binding.tvStep2.apply {
            setBackgroundColor(ContextCompat.getColor(context,R.color.white))
            setTextColor(ContextCompat.getColor(context,R.color.c_1A1A1A))
        }
        binding.tvStep3.apply {
            setBackgroundColor(ContextCompat.getColor(context,R.color.white))
            setTextColor(ContextCompat.getColor(context,R.color.c_1A1A1A))
        }

        // Set selected state
        when (selectedIndex) {
            0 -> binding.tvStep1
            1 -> binding.tvStep2
            2 -> binding.tvStep3
            else -> null
        }?.apply {
            setBackgroundResource(R.drawable.bg_step_selected)
            setTextColor(ContextCompat.getColor(context,R.color.white))
        }
    }
} 